<?php
/**
 * Created by PhpStorm.
 * User: w
 * Date: 2016/5/26r
 * Time: 15:47
 */

//会议管理
class Meet_managerController extends  BaseController{
    function index(){
        $do_meet = new Do会议表();
        $do_group = new Do群详情();
        $do_qrcode = new DoQrcode();
        $do_user = new Do用户();
        $meet_video = new Do会议视频();
        $do_meet->leftJoin($do_group,'b','a.id=b.父id')->leftJoin($do_user,'c','b.群主id=c.id');
        $count = $do_meet->count();
        $page = new  \tools\Page($count,15);
        $meet_res = $do_meet->fields('a.*,b.id gid,b.群主id,b.父id,c.name username')->offset($page->limit)->limit($page->pagesize)->orderby('a.id desc')->rows();
        foreach ( $meet_res as $k =>$v ) {
            //中国医师协会第六次全国新生儿科医师大会：有几个分会场的二维码，一对多
            if($v['name']=='中国医师协会第六次全国新生儿科医师大会'){
                $res_g = $do_group->fields('群名称,id,群主id,父id')->where('父id=1')->rows();
                foreach($res_g as $k1 =>$v1){
                    $where = " a.expire_time is NULL and a.note= ".$v1['id']." and a.user_id = ".$v1['群主id'];
                    $ress = $do_qrcode->fields('id,user_id,note,code')->where($where)->groupby('note')->rows();

                    foreach($ress as $k2 =>$v2){
                        $arr[] = $v2['code'];
                    }
                }
                $meet_res[$k]['qrcode']=$arr;


            }
            //存在讨论组，一般会议与二维码一对一
            if(!empty($v['gid'])){
                    $where = " expire_time is NULL and note= ".$v['gid']." and user_id = ".$v['群主id'];
                    $do_qrcode->fields('code')->where($where)->row();
                    if($do_qrcode->get_code()){
                        $meet_res[$k]['qrcode'][] = $do_qrcode->get_code();

                    }
            }
            //判断会议是否有视频
            $video_count = $meet_video->where(array('meet_id'=>$v['id']))->count();
            $meet_res[$k]['video_count'] = $video_count;
        }
        $url_form = $this->url('meet_manager','form_meet');
        $video_edit_url = $this->url('meet_manager','video_edit');
        $video_list_url = $this->url('meet_manager','video_list');
        $registration_list_url = $this->url('meet_manager','registration_list');
        $ico_url = $this->url('meet_manager','meet_ico_list');
        $meet_data_list = $this->url('meet_manager','meet_data_list');

        require $this->template;
    }

    //会议表单提交处理
    function form_meet(){
        $meet_do = new Do会议表();
        if($_POST){
            $name = $this->request('title','',true);
            $state = $this->request('状态');
            $max_register = $this->request('max_register');
            $homepage = $this->request('下一步','',true);
            $专家简介 = $this->request('专家简介');
            $会议地点 = $this->request('会议地点','',true);
            $会议时间 = $this->request('会议时间','',true);
            $会议议程 = $this->request('会议议程');
            $会议报名 = $this->request('会议报名');
            $会议简介 = $this->request('会议简介');
            $主办单位 = $this->request('主办单位');
            $子栏目 = $this->request('子栏目');
            $图片 = $this->request('图片');
            $操作人id = $this->request('操作人id');
            $news_first = $this->request('news_first', '');
            $news_remark = $this->request('news_remark', '');
            $live_video_time = $this->request('live_video_time', '');
            $news_url = $this->request('news_url', '');

            //上传图片开始
            if($_FILES['pic']['name']) {
                $img_url = 'cache/meet_img/';
                $img = Tools::upload_img($_FILES, $img_url,'meet_');
            }

            if($_FILES['new_pic_url']['name']) {
                $img_url = 'cache/meet_img/';
                $new_pic_url = Tools::upload_img($_FILES, $img_url,'meet_news_','new_pic_url');
            }

            if($img == '文件上传类型错误!' ||$img == '图片上传失败!' ||$img == '图片大小超过1M!') {
                echo "<script>alert('上传失败');</script>";
                return false;
            }
            //上传图片结束
            $meet_do->set_id($this->request('id'));
            $meet_do->where($meet_do->data)->row();
            $meet_do->set_name($name);
            $meet_do->set_状态($state);
            $is_creat_group=$meet_do->get_is_create_group()==1?'1':$this->request('is_create_group');
            $meet_do->set_max_register($max_register);
            $meet_do->set_下一步($homepage);
         //   $meet_do->set_专家简介($专家简介);
            $meet_do->set_会议地点($会议地点);
            $meet_do->set_会议时间($会议时间);
            $meet_do->set_会议议程($会议议程);
            $meet_do->set_会议报名($会议报名);
           // $meet_do->set_会议简介($会议简介);
            $meet_do->set_主办单位($主办单位);
            $meet_do->set_子栏目($子栏目);
            $meet_do->set_操作人id($操作人id);
            $img=empty($_FILES['pic']['name'])?$图片:$img;
            $meet_do->set_图片log($img);
            $meet_do->set_is_create_group($is_creat_group);
            $meet_do->set_news_first($news_first);
            $meet_do->set_news_remark($news_remark);
            $meet_do->set_live_video_time($live_video_time);
            $meet_do->set_news_url($news_url);
            isset($new_pic_url) && $meet_do->set_new_pic_url($new_pic_url);

            if($meet_do->save()){
                if($this->request('is_create_group')=='1'){
                    //创建群组
                    $qun = new C群聊天('');
                    //参数1:群主id,参数2：会议id==父id
                    $qun_id = $qun->fj建课程群($操作人id,$meet_do->get_id());
                }
                $url = $this->url('meet_manager','index');
                echo "<script>alert('提交成功!');location.href='".$url."';</script>";
            }else{
                echo "<script>alert('提交失败！')</script>";
            }
        }else{

            if($_REQUEST['id']){
                $huiyi_one = $meet_do->row($_REQUEST['id']);
                $huiyi_one = $huiyi_one->data;
            }
            require $this->template;
        }

    }

    #会议视频列表
    function video_list() {
        $do_meet_video = new Do会议视频();
        $do_meet = new Do会议表();
        $where = array('a.meet_id'=>$this->request('id'));
        $do_meet_video->leftJoin($do_meet, 'b', 'b.id=a.meet_id');
        $meet_video_count = $do_meet_video->fields('count(a.id)')->where($where)->getOneField();
        $page_meet_video = new tools\Page($meet_video_count, 15);
        $meet_video_list = $do_meet_video
            ->fields('a.*,b.name hname')
            ->where($where)
            ->offset($page_meet_video->limit)
            ->limit($page_meet_video->pagesize)
            ->orderby(' id desc')
            ->rows();
        $video_edit_url = $this->url('meet_manager','video_edit');
        $video_del_url = $this->url('meet_manager','video_del');
        require $this->template;
    }

    #添加编辑会议视频
    function video_edit() {
        $do_meet=new Do会议表();
        $do_meet_video=new Do会议视频();
        $param = array('id'=>$_GET['id']);
        if($_POST){
            //post 数据处理
            $res = $this->video_edit_post();
            $video_list_url=$this->url('meet_manager','video_list',$param);
            if($res){
                echo "<script>alert('提交成功');location.href='".$video_list_url."'</script>";
            }else{
                echo "<script>alert('提交失败');</script>";
            }
        }else{
            $huiyi_res=$do_meet->rows();
            if($_REQUEST['video_id']){
                $do_meet_video->leftJoin($do_meet,'b','b.id=a.meet_id');
                $huiyi_shipin_detail=$do_meet_video->fields('a.*,b.id yid ,b.name name')->where(array('a.id'=>$_REQUEST['video_id']))->rows();
            }
            require  $this->template;
        }


    }

    function video_edit_post() {
        $do_meet=new Do会议表();
        $do_meet_video=new Do会议视频();
        //会议表编号
       // $meet_name=$this->request('会议名称','',true);
      //  $a=explode('-',$meet_name);
        $meet_id = $_REQUEST['id'];
        $title = $this->request('title','', true);
        $url = $this->request('url','', true);

        $主讲人 = $this->request('主讲人','', true);
        $播放时长 = $this->request('播放时长','', true);
        $nowtime = $this->request('nowtime','', true);

        if(array_key_exists('群id', $_REQUEST) and $_REQUEST['群id']){
            $do_meet_video->set_群id($_REQUEST['群id']);
            $do_meet_video->where($do_meet_video->data)->row();

        } else {
            $操作人id = $this->request('操作人id',"",true);

            //创建群组
            $qun = new C群聊天('');
            $qun_id = $qun->fj建课程群($操作人id);

            $do_meet_video->set_群id($qun_id);
            $do_meet_video->set_操作人id($操作人id);
        }

        $do_meet_video->set_title($title);
        $do_meet_video->set_nowtime($nowtime);
        $do_meet_video->set_播放时长($播放时长);
        $do_meet_video->set_主讲人($主讲人);
        $do_meet_video->set_meet_id($meet_id);
        $do_meet_video->set_url($url);
        return $do_meet_video->save();

    }

    //会议视频删除
    function video_del(){
        $do_video = new Do会议视频();
        $do_video->set_id($_REQUEST['video_id']);
        $do_video->delete();
        $url = $this->url('meet_manager','video_list',array('id'=>$_REQUEST['id']));
        echo "<script>alert('删除成功!');location.href='".$url."';</script>";
    }

    #会议报名列表
    function registration_list() {
        $do_meet = new Do会议表();
        $do_meet_reg = new DoMeet_registration();//会议报名主表
        $do_meet_reg_af = new DoMeet_register_affiliate();//会议报名附属表--上海会议
        //下拉框的选择
        $where_meet = " 状态='发布' and 会议报名!='无'";
        $huiyi_res = $do_meet->where($where_meet)->orderby('id desc')->rows();
        //首都会议————第七届全国新生儿疾病诊疗进展学术研讨会
        //所有报名人数
        $do_meet_reg->leftJoin($do_meet,'b','a.会议id=b.id');
        $where = array('b.name'=>'第七届全国新生儿疾病诊疗进展学术研讨会');
        $shoudu_count = $do_meet_reg->fields('count(a.id)')->where($where)->getOneField();
        $shoudu_page = new \tools\Page($shoudu_count,15);
        $shoudu_res = $do_meet_reg->fields('a.*,b.name hname')->where($where)->offset($shoudu_page->limit)->limit($shoudu_page->pagesize)->orderby("a.id  desc")->rows();

        //上海会议--危重早产儿的救治与管理学习班
        $do_meet_reg->leftJoin($do_meet_reg_af,'c','a.id=c.meet_reg_id');
        $where_shanghai = array('b.name'=>'危重早产儿的救治与管理学习班');
        $shanghai_count = $do_meet_reg->fields('count(a.id)')->where($where_shanghai)->getOneField();
        $shanghai_page = new \tools\Page($shanghai_count,15);
        $shanghai_show=$do_meet_reg->fields(' a.* ,c.id cid,c.city,c.mailing_address,c.postcode,c.room_type,c.stay_time')->where($where_shanghai)->offset($shanghai_page->limit)->limit($shanghai_page->pagesize)->rows();
        require $this->template;
    }


    //图标列表
    function  meet_ico_list(){
        $do_col = new DoMeet_ico();
        $ico_list = $do_col->orderby('id desc')->rows();
        $arr = array();
        foreach($ico_list as $k =>$v){
            foreach($v as $a =>$b){
                if(strstr($b,'cache')){
                    $arr[] = $b;
                }

            }
        }
        $ico_edit_url =$this->url('meet_manager','meet_ico_edit');
        if(isset($_POST['method'])){
          echo  json_encode($arr);
        }else{
            require $this->template;
        }


    }

    //会议主页图标增、改
    function meet_ico_edit(){
        $do_ico =  new DoMeet_ico();
        if($_POST){
            $do_ico->set_id($_GET['id']);
            $do_ico->set_color($this->request('color','',true));
            //上传图片开始
            $img_url = 'cache/meet_img/';
            $imgs = Tools::upload_imgs($_FILES, $img_url,'pic','meet_ico_');
            //上传图片结束
            $ico1=$this->request('ico1');
            $img1 = empty($_FILES['pic']['name'][0])?$ico1:$imgs[0];
            $do_ico->set_ico1($img1);

            $ico1=$this->request('ico2');
            $img1 = empty($_FILES['pic']['name'][1])?$ico1:$imgs[1];
            $do_ico->set_ico2($img1);

            $ico2=$this->request('ico3');
            $img2 = empty($_FILES['pic']['name'][2])?$ico2:$imgs[2];
            $do_ico->set_ico3($img2);

            $ico3=$this->request('ico4');
            $img3 = empty($_FILES['pic']['name'][3])?$ico3:$imgs[3];
            $do_ico->set_ico4($img3);

            $ico4=$this->request('other');
            $img4 = empty($_FILES['pic']['name'][4])?$ico4:$imgs[4];
            $do_ico->set_other($img4);
           if($do_ico->save()){
               $this->redirect('meet_manager','meet_ico_list');
           }

        }else{
            if($_GET['id']){
                $do_ico->set_id($_GET['id']);
                $ico_detail = $do_ico->where($do_ico->data)->row();
            }
            $ico_list_url =$this->url('meet_manager','meet_ico_list');
            require $this->template;
        }
    }

    //会议资料数据处理--子栏目
    function meet_data_edit(){
        $do_meet = new Do会议表();
        $do_columnlist = new DoMeet_columnlist();
        $meet_id = $this->request('h_id');
        $do_meet->set_id($meet_id);
        $type = array('讨论组','会议资料','会议视频','报名');
        if($_POST){
            //上传图片开始
            $img_url = 'cache/meet_img/';
            $imgs = Tools::upload_img($_FILES, $img_url,'meet_ico_');
            //上传图片结束
            $title = $this->request('title','',true);
            $id = $this->request('id');
       //     $con = htmlspecialchars($this->request('content'));
            $do_columnlist = new DoMeet_columnlist();
            $do_columnlist->set_id($id);

            $do_columnlist->where($do_columnlist->data)->row();
            //file类型上传图片 优先
            $a = empty($_FILES['pic']['name'])?$_REQUEST['ico_hidden']:$imgs;
            $ico = empty($a)?$do_columnlist->get_ico():$a;
            $do_columnlist->set_title($title);
            $do_columnlist->set_meet_id($this->request('h_id'));
            $do_columnlist->set_url($this->request('url'));
          //  $do_columnlist->set_content($con);
            $do_columnlist->set_type($this->request('type'));
            $do_columnlist->set_ico($ico);

            if($do_columnlist->save()){
                $url = $this->url('meet_manager','meet_data_list',array('id'=>$meet_id));
                echo "<script>alert('提交成功!');location.href='".$url."';</script>";
            }
        }else{
            if(isset($_GET['id'])){
                $c_list = $do_columnlist->where(array('id'=>$_GET['id']))->row();
            }
            $ajax_ico_list = $this->url('meet_manager','meet_ico_list');
            $ico_edit_url =$this->url('meet_manager','meet_ico_edit');
            require $this->template;
        }


    }

    //会议资料列表
    function meet_data_list(){
        $do_column = new DoMeet_columnlist();
        $do_column->set_meet_id($this->request('id'));
        $column_list = $do_column->where($do_column->data)->rows();
        $data_edit_url = $this->url('meet_manager','meet_data_edit');
        require $this->template;
    }

    /**
     * 发送会议视频消息
     */
    function push_meet_video_message(){
        $bm = new C发微信消息();
        $infoId = $this->request('id','',true);
        $openId = $this->request('oid');
            if ($openId) {
                $msg = '测试通过';
                $bm->send_meet_video_message($infoId,$openId);
            } else {
                $msg = '后台促发完毕';
                $this->run_cli('meet_video_msg', $infoId);
            }
            $this->jsonOutPut(0, $msg);

    }

    /**
     * 发送会议消息
     */
    function push_meet_message() {
        $infoId = $this->request('id','',true);
        $openId = $this->request('oid');

        if ($openId) {
            $msg = '测试通过';

            $doUser = new Do用户();
            $doUser->set_openID($openId);
            $re = $doUser->fields("name, openID, id")->where($doUser->data)->row();
            if (!$re) {
                $this->jsonOutPut(-1, 'openId错误');
            }

            $news = new \bs\wxNews\Conference('meet', $infoId);
            $news->sendNews($doUser->data);

        } else {
            $msg = '后台促发完毕';
            $this->run_cli('meet_msg', $infoId);
        }
        $this->jsonOutPut(0, $msg);
    }

}